package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.annotation.keyWord;
import com.mall.db.domain.Goods;
import com.mall.db.service.GoodsService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods/gc")
public class GoodsController extends BaseController<Goods>{

    @Resource(name = "goodsService")
    private GoodsService gs;

    @Override
    public Map<String, Object> find(Goods goods) {
        PageHelper.startPage(goods.getPage(),goods.getLimit());
        List<Goods> list=gs.list();
        PageInfo<Goods> page=new PageInfo<>(list,goods.getLimit());
        return findMap(page.getList(),page.getTotal());
    }
    @RequestMapping("/seachFind")
    @keyWord
    public Map<String, Object> seachFind(Goods goods) {
        PageHelper.startPage(goods.getPage(),goods.getLimit());
        QueryWrapper qw=new QueryWrapper();
        if(!StringUtils.isEmpty(goods.getId()))
        {
            qw.eq("id",goods.getId());
        }
        if(!StringUtils.isEmpty(goods.getId()) && !StringUtils.isEmpty(goods.getName())){
            qw.or();
        }
        if(!StringUtils.isEmpty(goods.getName()))
        {
            qw.like("name",goods.getName());
        }
        List<Goods> list=gs.list(qw);
        PageInfo<Goods> page=new PageInfo<>(list,goods.getLimit());
        return findMap(page.getList(),page.getTotal());
    }

    @Override
    public Map<String, Object> update(Goods goods) {
         QueryWrapper qw=new QueryWrapper();
        boolean b=false;
         if(!StringUtils.isEmpty(goods.getStatus())){
             goods.setStatus(2);
         }
         if(!StringUtils.isEmpty(goods.getId())) {
              b = gs.updateById(goods);
         }
        return iud("修改",b,goods);
    }

    @RequestMapping("/add1")
    public Map<String, Object> cadd(Goods goods) {
        goods.setStatus(1);
         boolean b =  gs.save(goods);
         return this.iud("新增",b,goods);
    }
    @RequestMapping("/updata")
    public Map<String, Object> updata(Goods goods) {
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.eq("id",goods.getId());


       boolean b= gs.update(goods,qw);
       return iud("修改",b,goods);
    }



    @RequestMapping("/order/find")
    public Map<String, Object> orderFind(Goods goods) {
        PageHelper.startPage(goods.getPage(),goods.getLimit());
        QueryWrapper qw=new QueryWrapper();
        if(!StringUtils.isEmpty(goods.getId())) {
            qw.eq("id",goods.getId());
        }
        List<Goods> list=gs.list(qw);
        PageInfo<Goods> page=new PageInfo<>(list,goods.getLimit());
        return findMap(page.getList(),page.getTotal());
    }
}
